/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p1;

import java.util.ArrayList;

/**
 *
 * @author Derek Ward
 * 
 * Represents a node (sometimes referred internally
 * as a city) which contains references to it's 
 * edges, previous node (once a path is built),
 * it's label and a minimum distance from the 
 * source node
 * 
 */
public class Node implements Comparable<Node>
{
    private         String              m_label;
//    public          boolean             m_hasBeenVisited;
    public          double              m_min;
    public          ArrayList<Edge>     m_connectedEdges;
    public          Node                m_previousNode;
    
    public Node( String l )
    {
        m_label = l;
//        m_hasBeenVisited = false;
        m_min = Double.POSITIVE_INFINITY;
        m_previousNode = null;
        m_connectedEdges = new ArrayList<Edge>();
    }
    
    public void setLabel(String l)
    {
        if ( l != null )
            m_label = l;
        else
            throw new NullPointerException("Node::setLabel(String) was passed a null string!");
    }
    
    public String getLabel()
    {
        return m_label;
    }
    
    public int compareTo(Node n)
    {
        return Double.compare(m_min, n.m_min);
    }
    
    public int getEdgeWeight(Node n)
    {
        double weight = Double.POSITIVE_INFINITY;
        
        for ( Edge e : m_connectedEdges )
        {
            if ( e.getDestinationNode().getLabel().equals(m_label) )
            {
                if ( e.getSourceNode().getLabel().equals(n.getLabel()) )
                {
                    if ( e.getWeight() < weight )
                        weight = e.getWeight();
                }
            }
            else if ( e.getSourceNode().getLabel().equals(m_label) )
            {
                if ( e.getDestinationNode().getLabel().equals(n.getLabel()) )
                {
                    if ( e.getWeight() < weight )
                        weight = e.getWeight();
                }
            }
        }
        
        return (int)weight;
    }
    
    public void reset()
    {
        m_min = Double.POSITIVE_INFINITY;
        m_previousNode = null;
    }
}
